package cn.edu.sjtu.rfa;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcel;
import android.telephony.PhoneStateListener;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import randoop.main.*;

public class RandoopForAndroidActivity extends Activity {
	
	public static Context rc = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rc = this;

        final Button button_1 = (Button) findViewById(R.id.button_1);
        button_1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String[] args = {"gentests", 
                		"--classlist=/data/class_1.txt", 
                		"--componentfile-txt=/data/seedSequence.txt", 
                		"--log=/data/rLog.txt"};
            	//String[] args = {"gentests", "--testclass=android.app.Activity", "--literals-level=CLASS", "--literals-file=/data/literals.txt", "--forbid-null=false", "--log=/data/rLog.txt"};
            	List<String> arg = new ArrayList<String>();
            	Collections.addAll(arg, args);
            	EditText et = (EditText)findViewById(R.id.et);
            	if (!et.getText().toString().equals("")) {
                	String[] input = et.getText().toString().split(" ");
                	Collections.addAll(arg, input);
            	}
            	
            	String[] param = new String[arg.size()];
            	arg.toArray(param);
            	
                Main main = new Main();
                main.nonStaticMain(param);
            }
        });
    }
    
    public static Context getCurrentContext() {
    	return rc;
    }
    
    public static WifiManager getWifiManager() {
    	if (rc != null) {
    		return (WifiManager) rc.getSystemService(Context.WIFI_SERVICE);
    	} else {
    		return null;
    	}
    }
    
    public static Parcel getParcel() {
    	return Parcel.obtain();
    }
    
    public static AccountManager getAccountManager() {
    	return AccountManager.get(rc);
    }
    
    public static ActivityManager getActivityManager() {
    	return (ActivityManager) rc.getSystemService(Context.ACTIVITY_SERVICE);
    }
    
    public static AppWidgetManager getAppWidgetManager() {
    	return AppWidgetManager.getInstance(rc);
    }
    
    // TODO
    public static BluetoothDevice getBluetoothDevice() {
    	return BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:00:00:00:00:00");
    }
    
    public static BluetoothSocket getBluetoothSocket() throws IOException {
    	return getBluetoothDevice().createRfcommSocketToServiceRecord(UUID.randomUUID());
    }
    
    public static BluetoothServerSocket getBluetoothServerSocket() throws IOException {
    	return BluetoothAdapter.getDefaultAdapter().listenUsingRfcommWithServiceRecord("SDP", UUID.randomUUID());
    }
    
    public static ScanResult getScanResult() {
    	WifiManager wifiMgr = (WifiManager) rc.getSystemService(Context.WIFI_SERVICE);
    	if(wifiMgr.isWifiEnabled()) {
    	  List<ScanResult> scanResults = wifiMgr.getScanResults();
    	  if (scanResults != null && scanResults.size() > 0) {
    		  return scanResults.get(0);
    	  }
    	}
    	return null;
    }
    
    public static Editable.Factory getEditableFactory() {
		return Editable.Factory.getInstance();
    }
    
    public static Editable getEditable() {
		return getEditableFactory().newEditable("");
    }
    
    public static Intent getWebIntent() {
    	return new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
    }
    
    public static Intent getDialIntent() {
    	return new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0800000123"));
    }
    
    public static Intent getMMSIntent() {
    	return new Intent(Intent.ACTION_SEND, Uri.parse("content://media/external/images/media/23"));
    }
    
    public static Intent getSMSIntent() {
    	return new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto://0800000123"));
    }
    
    // TODO: can't get the instance without exception "Can't create handler inside thread that has not called Looper.prepare()"
    public static PhoneStateListener getPhoneStateListener() {
    	Looper.prepare();	// TODO can't close
    	return new PhoneStateListener();
    }
}